$(function () {
    myGetAjax(
        "/catering_system_app/user/getUser",
        {},
        getMyUser
    );
    listShopName();
    listname();
    $("#logout_1").click(function () {
        alert("11");
        logout();
    })
});

function getMyUser(RepVo) {
    if (RepVo.code == "200") {
        $("#myfun1_1").text(RepVo.data.userName);
        $("#myfun1_2").text(RepVo.data.phone);
        $("#myfun1_3").text(RepVo.data.sex);
        $("#myfun1_4").text(RepVo.data.role.roleName)
        $("#ImgUrl").attr("src","/catering_system_app/images/"+RepVo.data.imgUrl)
    } else {
        alert(RepVo.msg)
    }
    $(".myfun").click(function () {
        var str = "";
        $(".myfun").attr("style", "background:white");
        $(this).attr("style", "background:#83c3ff")
        if ($(this).text() == "个人中心") {
            myGetAjax(
                "/catering_system_app/user/getUser",
                {},
                getMyUser
            );
            str = " <form id='form_1'>     <div class='form-group'>" +
                "         <label>用&nbsp;户&nbsp;名：</label><span id='myfun1_1'>" + RepVo.data.userName + "</span>" +
                "       </div>\n" +
                "       <div class='form-group'>" +
                "          <label>手机号码：</label><spa id='myfun1_2'>" + RepVo.data.phone + "</spa>" +
                "       </div>" +
                "       <div class='form-group'>" +
                "          <label>性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别 ：</label><span id='myfun1_3'>" + RepVo.data.sex + "</span>" +
                "       </div>" +
                "       <div class='form-group'>" +
                "          <label>用户类别 ：</label><span id='myfun1_4'>" + (RepVo.data.role.roleName) + "</span>" +
                "       </div></form> ";
            $("#row_2_1_2").html(str);
        } else if ($(this).text() == "个人信息编辑") {
            myGetAjax(
                "/catering_system_app/user/getUser",
                {},
                getMyUser
            );
            str = " <form id='form_1'> <div class='form-group'>" +
                "       <label>用户名</label>" +
                "       <input type='text' class='form-control' name='username' id='username'>" +
                "       <span></span>" +
                "</div>" +
                "<div class='form-group'>" +
                "       <label>手机号码</label>" +
                "       <input type='text' class='form-control' name='phone' id='phone'>" +
                "</div>" +
                "<div class='form-group'>" +
                "        <label>性别 :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>" +
                "        <input type='radio' name='sex' id='man' value='男'> &nbsp;&nbsp;男&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "        <input type='radio' name='sex' id='woman' value='女'>&nbsp;&nbsp;女" +
                "</div>" +
                "<div class='form-group'>" +
                "         <button type='button' class='btn btn-default btn-lg btn-warning btn-block' id='editUser'>保存</button>" +
                "</div></form>";
            $("#row_2_1_2").html(str);
            $("#username").attr("value", RepVo.data.userName);
            $("#phone").attr("value", RepVo.data.phone);
            if (RepVo.data.sex == "男") {
                $("#man").prop("checked", true);
            } else {
                $("#woman").prop("checked", true);
            }
        } else if ($(this).text() == "修改密码") {
            myGetAjax(
                "/catering_system_app/user/getUser",
                {},
                getMyUser
            );
            str = "<form id='form_1'> <div class='form-group'>" +
                "      <label>旧密码</label>" +
                "      <input type='text' class='form-control' name='oldPassword' id='oldPassword'>" +
                "      <span id='password_check'></span>" +
                "</div>" +
                "<div class='form-group'>" +
                "      <label>新密码</label>" +
                "      <input type='password' class='form-control' class='newPassword' id='newPassword'>" +
                "</div>" +
                "<div class='form-group'>" +
                "      <label>新密码确认</label>" +
                "      <input type='password' class='form-control' class='newPassword' id='passwordTwo'>" +
                "      <span id='checkNewPassword'></span>" +
                "</div>" +
                "<div class='form-group'>" +
                "      <button class='btn btn-default btn-lg btn-warning btn-block' id='rePassword'>修改</button>" +
                "</div></form>";
            $("#row_2_1_2").html(str);
        } else if ($(this).text() == "上传图像") {
            str = " <img class='user-header' style='width: 100px;height: 134px;object-fit: cover;' src='/catering_system_app/images/"+RepVo.data.imgUrl+"'>"+
                "  <form method='post' enctype='multipart/form-data'>" +
                "       <input type='file' id='file' name='file' onchange='imgChange(event)'>" +
                "    </form>\n" +
                "       <button data-role='none' onclick='testUpload()' class='btn btn-info'>保存图片</button>";
            $("#row_2_1_2").html(str);
        }
        $("#oldPassword").blur(function () {
            myGetAjax(
                "/catering_system_app/user/checkPassword",
                {
                    password: $("#oldPassword").val()
                },
                checkPassword
            )
        });
        $("#passwordTwo").blur(function () {
            if ($("#passwordTwo").val() == $("#newPassword").val()) {
                $("#checkNewPassword").attr("style", "color:green");
                $("#checkNewPassword").text("两次密码一致");
            } else {
                $("#checkNewPassword").attr("style", "color:red");
                $("#checkNewPassword").text("两次密码不一致")
            }
        });
        $("#rePassword").click(function () {
            myGetAjax(
                "/catering_system_app/user/changePassword",
                {
                    password: $("#newPassword").val()
                },
                changePassword
            )
        });
        $("#username").blur(function () {
            if (  $("#username").val()!=RepVo.data.userName){
                myGetAjax(
                    "/catering_system_app/user/checkUser",
                    {
                        userName:$("#username").val()
                    },
                    checkUser
                )
            }
        });
        $("#editUser").click(function () {
            myGetAjax(
                "/catering_system_app/user/changePassword",
                {
                    userName:$("#username").val(),
                    phone:$("#phone").val(),
                    sex:$("input[name='sex']:checked").val(),
                },
                editUser
            )
        })
    })
}

function checkPassword(RespVo) {
    if (RespVo.code == "200") {
        $("#password_check").attr("style", "color:green");
        $("#password_check").text("旧密码正确")
    } else {
        $("#password_check").attr("style", "color:red");
        $("#password_check").text(RespVo.msg)
    }
}

function changePassword(RespVo) {
    if (RespVo.code == "200") {
        alert("密码修改成功");
        /* location.href="index.html";*/
    } else {
        alert(RespVo.msg)
    }
}
function checkUser(RespVo) {
    if (RespVo.code=="100") {
        $("span:eq(0)").attr("style", "color:red");
        $("span:eq(0)").text("用户名已存在");
    }else {
        $("span:eq(0)").attr("style", "color:green");
        $("span:eq(0)").text("用户名可用");
    }
}
function editUser(RespVo) {
    if (RespVo.code=="200"){
        alert(RespVo.data);
    }else {
        alert(RespVo.msg);
    }
}
function imgChange(e) {
    console.info(e.target.files[0]);//图片文件
    console.log(e.target.value);//这个也是文件的路径和上面的dom.value是一样的
    var reader = new FileReader();
    reader.onload = (function (file) {
        return function (e) {
            $('.user-header').attr('src',this.result);
        };
    })(e.target.files[0]);
    reader.readAsDataURL(e.target.files[0]);
}
function testUpload(){
    var form = new FormData();
    form.append('file',$("#file")[0].files[0]);
    $.ajax({
        url:"/catering_system_app/user/addImgUrl",
        data:form,
        type:"POST",
        processData:false,
        contentType:false,
        success : function(RespVo){
            if(RespVo.code=="200"){
                alert("成功");
                $("#ImgUrl").attr("style","width:100px;height:134px")
                $("#ImgUrl").attr("src","/catering_system_app/images/"+RepVo.data.imgUrl)

            }else{
                alert(RespVo.msg);
            }
        },
    });
}